package com.hnust.controller.personInfo;

import com.hnust.controller.paper.paperManagement.PaperResultController;
import com.hnust.domain.ChapterData;
import com.hnust.domain.ChapterDataRecord;
import com.hnust.domain.CourseData;
import com.hnust.fx.FXMLController;
import com.hnust.service.ChapterServices;
import com.hnust.service.SubjectServices;
import com.jfoenix.controls.JFXButton;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import javafx.util.StringConverter;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

/**
 * @Author: Yjx
 * @Data: 2021/10/27 16:44
 * @Version 1.0
 * @Project_Name: testTeacher
 * @describe
 */
@FXMLController
public class ChapterImportController implements Initializable {
    public Label courseDataLabel;
    public JFXButton downloadBtn;
    public JFXButton allImportBtn;
    public JFXButton uploadBtn;
    public HBox form;
    public VBox loadingUploadInfo;
    @FXML
    public TableView<ChapterList> TableView;
    @FXML
    public ScrollPane container;
    @FXML
    public AnchorPane contain;

    @Autowired
    ChapterServices chapterServices;

    @Autowired
    private SubjectServices subjectServices;

    @FXML
    ComboBox<CourseData> comb;
    private String selectCourseId;
    private final ObservableList<ChapterList> data = FXCollections.observableArrayList();
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        listenChange();
        initTable();
        comb.setConverter(new StringConverter<CourseData>() {
            @Override
            public String toString(CourseData object) {
                return object.getName();
            }

            @Override
            public CourseData fromString(String string) {
                return null;
            }
        });
        comb.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
            if(newValue!=null){
                selectCourseId = newValue.getId();
            }
        });
        container.parentProperty().addListener((observable, oldValue, newValue) -> {
            if (newValue != null) {
                getCombData();
            } else {
                clear();
                selectCourseId = null;
            }
        });
    }
    //获取下拉框数据
    public void getCombData() {
        subjectServices.getCourseData(courseDataLabel, comb,0,null);
    }

    public void listenChange(){
        //通过监听最外层容器的宽度，来改变内层Anchor的宽度
        container.widthProperty().addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
                contain.setPrefWidth((double)newValue-2);
            }
        });
        //通过监听最外层容器的高度，来改变内层Anchor的高度
        container.heightProperty().addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
                contain.setPrefHeight((double)newValue-2);
            }
        });
    }
    public void initTable(){
        TableColumn name = new TableColumn("章节内容");
        name.setMinWidth(400);
        name.setCellValueFactory(
                new PropertyValueFactory<>("name"));

        TableColumn chapter = new TableColumn("章");
        chapter.setMinWidth(250);
        chapter.setCellValueFactory(
                new PropertyValueFactory<>("chapter"));

        TableColumn frist = new TableColumn("一级节");
        frist.setMinWidth(250);
        frist.setCellValueFactory(
                new PropertyValueFactory<>("frist"));

        TableColumn second = new TableColumn("二级节");
        second.setMinWidth(250);
        second.setCellValueFactory(
                new PropertyValueFactory<>("second"));

        TableColumn third = new TableColumn("三级节");
        third.setMinWidth(250);
        third.setCellValueFactory(
                new PropertyValueFactory<>("third"));

        TableView.getColumns().addAll(name, chapter,frist,second,third);
    }
    public void chooseFile() {
        FileChooser fileChooser = new FileChooser();
        FileChooser.ExtensionFilter filter = new FileChooser.ExtensionFilter("Text Files", "*.doc", " *.docx");
        fileChooser.getExtensionFilters().add(filter);
        File file = fileChooser.showOpenDialog(null);
        if(file!=null){
            chapterServices.uploadFile(file,uploadBtn,selectCourseId,data,TableView,form,loadingUploadInfo);
        }
    }

    public void downloadFile(){
        chapterServices.downloadFile(downloadBtn);
    }

    public void cancel(){
        data.clear();
        form.setVisible(false);
    }

    public void allImport(){
        System.out.println("导入");
        List<ChapterData> list= new ArrayList<>();
        ChapterDataRecord chapterDataRecord = new ChapterDataRecord();
        chapterDataRecord.setList(list);
        data.forEach(chapterList -> {list.add(new ChapterData(chapterList.getName(),chapterList.getChapter(),chapterList.getFrist(),chapterList.getSecond(),chapterList.getThird()));});
        chapterServices.allImport(chapterDataRecord,selectCourseId,allImportBtn,data,form);
    }

    //清空此页面所有数据
    public void clear() {
        data.clear();
        form.setVisible(false);
        form.setManaged(false);
    }
    public static class ChapterList{
        private final SimpleStringProperty name;
        private final SimpleStringProperty chapter;
        private final SimpleStringProperty frist;//姓名
        private final SimpleStringProperty second;//班级
        private final SimpleStringProperty third;//总分I

        public ChapterList(String name, String chapter, String frist, String second, String third) {
            this.name = new SimpleStringProperty(name);
            this.chapter = new SimpleStringProperty(chapter);
            this.frist = new SimpleStringProperty(frist);
            this.second = new SimpleStringProperty(second);
            this.third = new SimpleStringProperty(third);
        }

        public String getName() {
            return name.get();
        }

        public SimpleStringProperty nameProperty() {
            return name;
        }

        public void setName(String name) {
            this.name.set(name);
        }

        public String getChapter() {
            return chapter.get();
        }

        public SimpleStringProperty chapterProperty() {
            return chapter;
        }

        public void setChapter(String chapter) {
            this.chapter.set(chapter);
        }

        public String getFrist() {
            return frist.get();
        }

        public SimpleStringProperty fristProperty() {
            return frist;
        }

        public void setFrist(String frist) {
            this.frist.set(frist);
        }

        public String getSecond() {
            return second.get();
        }

        public SimpleStringProperty secondProperty() {
            return second;
        }

        public void setSecond(String second) {
            this.second.set(second);
        }

        public String getThird() {
            return third.get();
        }

        public SimpleStringProperty thirdProperty() {
            return third;
        }

        public void setThird(String third) {
            this.third.set(third);
        }
    }
}
